package com.wave.wavesystem.serve.aop;

import com.wave.common.enums.ErrorCode;
import com.wave.common.exception.WaveException;
import com.wave.common.util.Func;
import com.wave.wavesystem.serve.annotation.LoginCheck;
import com.wave.wavesystem.serve.annotation.RoleCheck;
import com.wave.wavesystem.serve.pojo.entity.CurrentUser;
import com.wave.wavesystem.serve.util.CurrentUserUtil;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;

@Aspect
@Component
@LoginCheck
public class RoleCheckAop {

    @Around("@annotation(roleCheck)")
    public Object userRoleCheck(ProceedingJoinPoint joinPoint, RoleCheck roleCheck) throws Throwable {
        CurrentUser currentUser = CurrentUserUtil.getCurrentUser();

        if (Func.isEmpty(currentUser)) {
            throw new WaveException("当前用户无角色");
        }
        String code = roleCheck.value().getCode();
        if (!currentUser.getRole().contains(code)) {
            throw new WaveException(ErrorCode.AUTH_FAIL);
        }
        return joinPoint.proceed();
    }

}
